home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / frntsdk1.cpt / Frontier SDK 1.0 ƒ / Applet Toolkit / kb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-21  |  5.8 KB  |  307 lines

  1.  
  2. /*⌐ Copyright 1988-1991 UserLand Software, Inc.  All Rights Reserved.*/
  3.  
  4.  
  5.  
  6. #include "appletinternal.h"
  7. #include "ops.h"
  8. #include "kb.h"
  9.  
  10.  
  11.  
  12. #define keycodeclear 71 /*keycodes for numeric keybad*/
  13. #define keycodeminus 78
  14. #define keycodeplus 69
  15. #define keycodetimes 67
  16. #define keycodeseven 89
  17. #define keycodeeight 91
  18. #define keycodenine 92
  19. #define keycodedivide 77
  20. #define keycodefour 86
  21. #define keycodefive 87
  22. #define keycodesix 88
  23. #define keycodecomma 72
  24. #define keycodeone 83
  25. #define keycodetwo 84
  26. #define keycodethree 85
  27. #define keycodeenter 76
  28. #define keycodezero 82
  29. #define keycodeperiod 65
  30.  
  31.  
  32. tykeystrokerecord keyboardstatus; 
  33.  
  34. static boolean flescapepending = false;
  35.  
  36.  
  37.  
  38.  
  39. boolean arrowkey (char chkb) {
  40.     
  41.     /*
  42.     return true if the indicated key character is an arrow key.
  43.     */
  44.     
  45.     register char ch = chkb;
  46.     
  47.     return (
  48.         (ch == chuparrow) || (ch == chdownarrow) || 
  49.         
  50.         (ch == chleftarrow) || (ch == chrightarrow));
  51.     } /*arrowkey*/
  52.  
  53.  
  54. tydirection keystroketodirection (char ch) {
  55.     
  56.     switch (ch) {
  57.         
  58.         case chleftarrow:
  59.             return (left);
  60.             
  61.         case chrightarrow:
  62.             return (right);
  63.             
  64.         case chuparrow:
  65.             return (up);
  66.             
  67.         case chdownarrow:
  68.             return (down);
  69.         } /*switch*/
  70.         
  71.     return (nodirection);
  72.     } /*keystroketodirection*/
  73.     
  74.  
  75. static void kbsetstatus (EventRecord ev, tykeystrokerecord *kbs) {
  76.     
  77.     /*
  78.     12/11/90 dmb: don't count the caps lock as a modifier in ctmodifiers.
  79.     
  80.     2/21/91 dmb: handle calls for mousedown events.  we just want to 
  81.     record modifier keys
  82.     */
  83.     
  84.     register short ct;
  85.     register char chkb;
  86.     register short keycode;
  87.     tykeystrokerecord kbcurrent;
  88.     
  89.     kbcurrent.flshiftkey = (ev.modifiers & shiftKey) != 0;
  90.     
  91.     kbcurrent.flcmdkey = (ev.modifiers & cmdKey) != 0;
  92.     
  93.     kbcurrent.floptionkey = (ev.modifiers & optionKey) != 0;
  94.     
  95.     kbcurrent.flalphalock = (ev.modifiers & alphaLock) != 0;
  96.     
  97.     kbcurrent.flcontrolkey = (ev.modifiers & controlKey) != 0;
  98.     
  99.     ct = 0;
  100.     
  101.     if (kbcurrent.flshiftkey) ct++;
  102.     
  103.     if (kbcurrent.flcmdkey) ct++;
  104.     
  105.     if (kbcurrent.floptionkey) ct++;
  106.     
  107.     /*
  108.     if (kbcurrent.flalphalock) ct++;
  109.     */
  110.     
  111.     if (kbcurrent.flcontrolkey) ct++;
  112.     
  113.     kbcurrent.ctmodifiers = ct;    
  114.     
  115.     if (ev.what == mouseDown) {
  116.         
  117.         kbcurrent.chkb = chnul;
  118.         
  119.         kbcurrent.keycode = 0;
  120.         
  121.         kbcurrent.flautokey = false;
  122.         
  123.         kbcurrent.keydirection = nodirection;
  124.         }
  125.     else {
  126.         
  127.         kbcurrent.chkb = chkb = ev.message & charCodeMask; /*get the keystroke*/
  128.         
  129.         kbcurrent.flautokey = (ev.what == autoKey);
  130.         
  131.         kbcurrent.keycode = keycode = (ev.message & keyCodeMask) >> 8;
  132.         
  133.         kbcurrent.keydirection = keystroketodirection (chkb);
  134.         
  135.         if (kbcurrent.flcmdkey  &&  kbcurrent.floptionkey) {
  136.     
  137.             /*
  138.             we don't want to option character, so find the normal character from 
  139.             the keymap.  see IM V-195
  140.             */
  141.             
  142.             Handle hkchr;
  143.             long state = 0;
  144.             
  145.             if (hkchr = GetResource ('KCHR', 0))
  146.                 kbcurrent.chkb = KeyTrans (*hkchr, kbcurrent.keycode, &state) & 0x000000ff;
  147.             };
  148.     
  149.         kbcurrent.flkeypad = /*true if it is a keystroke from the numeric keypad*/
  150.             
  151.             (keycode == keycodeclear)         || (keycode == keycodeminus)     || 
  152.             
  153.             (keycode == keycodeplus)         || (keycode == keycodetimes)     ||
  154.             
  155.             (keycode == keycodeseven)         || (keycode == keycodeeight)     ||
  156.             
  157.             (keycode == keycodenine)         || (keycode == keycodedivide)     ||
  158.             
  159.             (keycode == keycodefour)         || (keycode == keycodefive)     ||
  160.             
  161.             (keycode == keycodesix)         || (keycode == keycodecomma)     ||
  162.             
  163.             (keycode == keycodeone)         || (keycode == keycodetwo)         ||
  164.             
  165.             (keycode == keycodethree)         || (keycode == keycodeenter)     ||
  166.             
  167.             (keycode == keycodezero)         || (keycode == keycodeperiod);
  168.         }
  169.     
  170.     *kbs = kbcurrent; /*set for caller*/
  171.     } /*kbsetstatus*/
  172.     
  173.  
  174. setkeyboardstatus (EventRecord ev) {
  175.     
  176.     /*
  177.     sets a global that may be referenced by anyone.
  178.     
  179.     should be called every time a new event is received.
  180.     */
  181.     
  182.     kbsetstatus (ev, &keyboardstatus); /*use global*/
  183.     } /*setkeyboardstatus*/
  184.  
  185.  
  186. keyboardclearescape (void) {
  187.     
  188.     flescapepending = false;
  189.     } /*keyboardclearescape*/
  190.  
  191.  
  192. keyboardsetescape (void) {
  193.     
  194.     /*ouch (); /*audible feedback asap, multimedia!*/
  195.     
  196.     /*
  197.     shellfrontrootwindowmessage ("\pCancelled.");
  198.     */
  199.     
  200.     flescapepending = true;
  201.     } /*keyboardsetescape*/
  202.  
  203.  
  204. boolean keyboardescape (void) {
  205.     
  206.     /*
  207.     check to see if the user has pressed cmd-period.  if not, no effect on
  208.     the event queue and we return false.
  209.     
  210.     otherwise, we remove the keystroke and return true.  the caller is expected
  211.     to return quickly!
  212.     */
  213.     
  214.     register unsigned long tc;
  215.     tykeystrokerecord kbcurrent;
  216.     EventRecord ev;
  217.     static unsigned long lastcheck = 0;
  218.     
  219.     if (flescapepending)
  220.         return (true);
  221.     
  222.     tc = TickCount ();
  223.     
  224.     if ((tc - 60) < lastcheck) /*check 1 time per second*/
  225.         return (false);
  226.     
  227.     lastcheck = tc; /*remember for next time*/
  228.     
  229.     if (EventAvail (keyDownMask, &ev)) {
  230.         
  231.         kbsetstatus (ev, &kbcurrent);
  232.         
  233.         if (kbcurrent.flcmdkey && (kbcurrent.chkb == '.')) {
  234.             
  235.             GetNextEvent (keyDownMask, &ev); /*get rid of the cmd-period*/
  236.             
  237.             keyboardsetescape (); /*multimedia!*/
  238.             
  239.             return (true);
  240.             }
  241.         }
  242.     else { /*potentially surrender the processor to background tasks*/
  243.         
  244.         /*WaitNextEvent (nullEvent, &ev, 1, nil);*/
  245.         }
  246.     
  247.     /*motorsound ();*/
  248.     
  249.     return (false);
  250.     } /*keyboardescape*/
  251.  
  252.  
  253. keyboardpeek (tykeystrokerecord *kbrecord) {
  254.     
  255.     register ptrkeystrokerecord p = kbrecord;
  256.     KeyMap keys;
  257.     
  258.     clearbytes (p, longsizeof (tykeystrokerecord));
  259.     
  260.     GetKeys (&keys);
  261.     
  262.     (*p).flshiftkey = BitTst (&keys, 63);
  263.         
  264.     (*p).flcmdkey = BitTst (&keys, 48);
  265.         
  266.     (*p).floptionkey = BitTst (&keys, 61);
  267.         
  268.     (*p).flcontrolkey = BitTst (&keys, 60);
  269.     } /*keyboardpeek*/
  270.  
  271.  
  272. static boolean keydown (short keycode) {
  273.  
  274.     KeyMap keys;
  275.     
  276.     GetKeys (&keys);
  277.     
  278.     return (BitTst (&keys, keycode));
  279.     } /*keydown*/
  280.     
  281.     
  282. boolean enterkeydown (void) {    
  283.     
  284.     return (keydown (75));
  285.     } /*enterkeydown*/
  286.     
  287.     
  288. boolean optionkeydown (void) {
  289.     
  290.     return (keydown (61));
  291.     } /*optionkeydown*/
  292.     
  293.     
  294. boolean cmdkeydown (void) {
  295.         
  296.     return (keydown (48));
  297.     } /*cmdkeydown*/
  298.     
  299.     
  300. boolean shiftkeydown (void) {
  301.         
  302.     return (keydown (63));
  303.     } /*shiftkeydown*/
  304.     
  305.     
  306.     
  307.